package es.deusto.eside.programacion4.mario2d.states;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.util.Log;

import es.deusto.eside.programacion4.mario2d.Mario2D;
import es.deusto.eside.programacion4.mario2d.Bd.database.GestorBD;
import es.deusto.eside.programacion4.mario2d.Bd.domain.Jugador;

public class RankingState extends BasicGameState {
	Image background;
	List<Jugador> lJugador;
	String nick;
	public int id;
	/**
	 * Fuente
	 */
	private UnicodeFont fpsFont;
	/**
	 * Ruta del estilo
	 */
	private static final String fontPath = "resources/ttf/mario.ttf";

	public RankingState(int ordinal) {
		this.id = ordinal;
	}

	@Override
	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException {
		nick = "";
		/*
		 * try { if( Mario2D.gBD.listJugador().size()>0) lJugador =
		 * Mario2D.gBD.listJugador(); } catch (SQLException e) { 
		 * Auto-generated catch block e.printStackTrace(); }
		 */
		fpsFont = new UnicodeFont(fontPath, 40, true, false);
		fpsFont.addAsciiGlyphs();
		fpsFont.addGlyphs(400, 600);
		fpsFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
		fpsFont.loadGlyphs();
		background = new Image("resources/Images/Ranking/star.jpg");
		background = background.getScaledCopy(Mario2D.WIDTH, Mario2D.HEIGHT);
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sb, Graphics g)
			throws SlickException {
		org.newdawn.slick.Color black = new org.newdawn.slick.Color(0, 0, 0,
				200);
		org.newdawn.slick.Color white = new org.newdawn.slick.Color(255, 255,
				255, 255);
		// Rect r1 = new Rect(3, Mario2D.HEIGHT-71 , 5, 5);
		// Rect r2 = new Rect(Mario2D.WIDTH-71, 3 , 5, 5);

		Rectangle rect = new Rectangle(38, 38, Mario2D.WIDTH - 76,
				Mario2D.HEIGHT - 76);
		// g.setFont(theme);
		g.drawImage(background, 0, 0);
		g.setColor(white);
		g.fillRect(33, 33, 5, Mario2D.HEIGHT - 71);
		g.fillRect(Mario2D.WIDTH - 38, 38, 5, Mario2D.HEIGHT - 76);
		g.fillRect(33, 33, Mario2D.WIDTH - 66, 5);
		g.fillRect(33, Mario2D.HEIGHT - 38, Mario2D.WIDTH - 66, 5);
		g.setColor(black);
		g.fill(rect);
		Image title = new Image("resources/Images/Option/title.png");
		g.drawImage(title, Mario2D.WIDTH / 2 - title.getWidth() / 2, 0);
		// g.drawString(Locale.INSTANCE.getText("Option"), 280, 30);
		fpsFont.drawString(70, 130, "Nick: ");
		fpsFont.drawString(240, 20, "Puntos");
		fpsFont.drawString(200, 200, nick);
		fpsFont.drawString(100, 300, "Puntos obtenidos");
		String puntos = "" + Mario2D.points;
		fpsFont.drawString(300, 360, puntos);

	}

	@Override
	public void update(GameContainer gc, StateBasedGame sb, int delta)
			throws SlickException {
		Input i = gc.getInput();
		if (nick.length() < 7) {
			if (i.isKeyPressed(Input.KEY_A)) {
				nick += "A";
			}
			if (i.isKeyPressed(Input.KEY_B)) {
				nick += "B";
			}
			if (i.isKeyPressed(Input.KEY_C)) {
				nick += "C";
			}
			if (i.isKeyPressed(Input.KEY_D)) {
				nick += "D";
			}
			if (i.isKeyPressed(Input.KEY_E)) {
				nick += "E";
			}
			if (i.isKeyPressed(Input.KEY_F)) {
				nick += "F";
			}
			if (i.isKeyPressed(Input.KEY_G)) {
				nick += "G";
			}
			if (i.isKeyPressed(Input.KEY_H)) {
				nick += "H";
			}
			if (i.isKeyPressed(Input.KEY_I)) {
				nick += "I";
			}
			if (i.isKeyPressed(Input.KEY_J)) {
				nick += "J";
			}
			if (i.isKeyPressed(Input.KEY_K)) {
				nick += "K";
			}
			if (i.isKeyPressed(Input.KEY_L)) {
				nick += "L";
			}
			if (i.isKeyPressed(Input.KEY_M)) {
				nick += "M";
			}
			if (i.isKeyPressed(Input.KEY_N)) {
				nick += "N";
			}
			if (i.isKeyPressed(Input.KEY_O)) {
				nick += "O";
			}
			if (i.isKeyPressed(Input.KEY_P)) {
				nick += "P";
			}
			if (i.isKeyPressed(Input.KEY_Q)) {
				nick += "Q";
			}
			if (i.isKeyPressed(Input.KEY_R)) {
				nick += "R";
			}
			if (i.isKeyPressed(Input.KEY_S)) {
				nick += "S";
			}
			if (i.isKeyPressed(Input.KEY_T)) {
				nick += "T";
			}
			if (i.isKeyPressed(Input.KEY_U)) {
				nick += "U";
			}
			if (i.isKeyPressed(Input.KEY_V)) {
				nick += "V";
			}
			if (i.isKeyPressed(Input.KEY_X)) {
				nick += "X";
			}
			if (i.isKeyPressed(Input.KEY_Y)) {
				nick += "Y";
			}
			if (i.isKeyPressed(Input.KEY_Z)) {
				nick += "Z";
			}
		}
		if (i.isKeyPressed(Input.KEY_BACK) && nick.length() > 0) {
			nick = nick.substring(0, nick.length() - 1);
		}
		if (i.isKeyPressed(Input.KEY_ENTER) && nick.length() > 0) {
			try {
				lJugador = Mario2D.gBD.listJugador();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			if (lJugador.size() < 5) {
				try {
					Mario2D.gBD.addJugador(new Jugador(nick, Mario2D.points));
				} catch (SQLException e) {
					e.printStackTrace();
				}
			} else {

				Jugador player1 = lJugador.get(lJugador.size() - 1);
				if (player1.getPuntos() < Mario2D.points) {
					Jugador player2 = new Jugador(nick, Mario2D.points);
					lJugador.set(lJugador.size() - 1, player2);
					try {
						Mario2D.gBD.deleteJugador(player1);
					} catch (SQLException e) {
						e.printStackTrace();
					}
					try {
						Mario2D.gBD.addJugador(player2);
					} catch (SQLException e) {
						e.printStackTrace();
					}
					Log.error("cambiado");
				}
			}
			init(gc, sb);
			sb.enterState(GameState.MENU_STATE.ordinal(),
					new FadeOutTransition(), new FadeInTransition());
		}
	}

	public void enter(final GameContainer gameContainer, final StateBasedGame sb) {
		if (Mario2D.p.isMusic() == true) {
			Mario2D.pole.loop();
		}

	}

	@Override
	public void leave(final GameContainer gameContainer, final StateBasedGame sb) {
		Mario2D.pole.stop();
	}

	@Override
	public int getID() {
		return this.id;
	}

}
